package io.geekidea.boot.yuangongqiandao.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.geekidea.boot.area.service.AreaService;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;

import io.geekidea.boot.store.service.StoreService;
import io.geekidea.boot.supplier.entity.Supplier;
import io.geekidea.boot.yuangongqiandao.dto.QianDaoQingKuangDto;
import io.geekidea.boot.yuangongqiandao.entity.QianDaoQingKuang;
import io.geekidea.boot.yuangongqiandao.service.IQianDaoQingKuangService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 签到情况 前端控制器
 * </p>
 *
 * @author xukang
 * @since 2023-11-26
 */
@RestController
@RequestMapping("/qiandao")
@Tag(name = "签到情况2")
public class QianDaoQingKuangController {
    @Resource
    private AreaService areaService;
    @Resource
    private StoreService storeService;
    @Resource
    private IQianDaoQingKuangService qianDaoQingKuangService;

    @PostMapping("/page")
    @Operation(tags = "Post", description = "222222222222222")
    public ApiResult getQianDaoList(@RequestBody QianDaoQingKuangDto qianDaoQingKuangDto) {
        Page<QianDaoQingKuang> page=new Page<>(qianDaoQingKuangDto.getPage(),qianDaoQingKuangDto.getSize());
        LambdaQueryWrapper<QianDaoQingKuang> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(qianDaoQingKuangDto.getShouJiHàomǎ()), QianDaoQingKuang::getShouJiHàomǎ, qianDaoQingKuangDto.getShouJiHàomǎ());
        queryWrapper.eq(StringUtils.isNotBlank(qianDaoQingKuangDto.getJuésè()), QianDaoQingKuang::getJuésè, qianDaoQingKuangDto.getJuésè());
        queryWrapper.between(Objects.nonNull(qianDaoQingKuangDto.getBenYueWeiQianDaoCìshùMin())
                        && Objects.nonNull(qianDaoQingKuangDto.getBenYueWeiQianDaoCìshùMax()), QianDaoQingKuang::getBenYueWeiQianDaoCìshù,
                qianDaoQingKuangDto.getBenYueWeiQianDaoCìshùMin(), qianDaoQingKuangDto.getBenYueWeiQianDaoCìshùMax());
        queryWrapper.eq(StringUtils.isNotBlank(qianDaoQingKuangDto.getSuoZaiZhànqū()), QianDaoQingKuang::getSuoZaiZhànqū, qianDaoQingKuangDto.getSuoZaiZhànqū());
        queryWrapper.eq(StringUtils.isNotBlank(qianDaoQingKuangDto.getSuoZaiMenDian()), QianDaoQingKuang::getSuoZaiMenDian, qianDaoQingKuangDto.getSuoZaiMenDian());
        Page<QianDaoQingKuang> pageDto=qianDaoQingKuangService.page(page,queryWrapper);
        PageDto<QianDaoQingKuang> pageInfo=new PageDto<>(pageDto.getRecords(),pageDto.getTotal(),pageDto.getSize(),pageDto.getCurrent());
        return ApiResult.success(pageInfo);
    }


    @PostMapping("/output")
    @Operation(tags = "Post", description = "输出excel")
    public void excelOutput(@RequestBody QianDaoQingKuangDto qianDaoQingKuangDto, HttpServletResponse response) {
        LambdaQueryWrapper<QianDaoQingKuang> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(qianDaoQingKuangDto.getShouJiHàomǎ()), QianDaoQingKuang::getShouJiHàomǎ, qianDaoQingKuangDto.getShouJiHàomǎ());
        queryWrapper.eq(StringUtils.isNotBlank(qianDaoQingKuangDto.getJuésè()), QianDaoQingKuang::getJuésè, qianDaoQingKuangDto.getJuésè());
        queryWrapper.between(Objects.nonNull(qianDaoQingKuangDto.getBenYueWeiQianDaoCìshùMin())
                        && Objects.nonNull(qianDaoQingKuangDto.getBenYueWeiQianDaoCìshùMax()), QianDaoQingKuang::getBenYueWeiQianDaoCìshù,
                qianDaoQingKuangDto.getBenYueWeiQianDaoCìshùMin(), qianDaoQingKuangDto.getBenYueWeiQianDaoCìshùMax());
        queryWrapper.eq(StringUtils.isNotBlank(qianDaoQingKuangDto.getSuoZaiZhànqū()), QianDaoQingKuang::getSuoZaiZhànqū, qianDaoQingKuangDto.getSuoZaiZhànqū());
        queryWrapper.eq(StringUtils.isNotBlank(qianDaoQingKuangDto.getSuoZaiMenDian()), QianDaoQingKuang::getSuoZaiMenDian, qianDaoQingKuangDto.getSuoZaiMenDian());
        List<QianDaoQingKuang> list = qianDaoQingKuangService.list(queryWrapper);

        OutputStream outputStream = null;
        try {
            response.setCharacterEncoding("UTF-8");
            String excelName = URLEncoder.encode("supplier_" + LocalDateTime.now(), "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + excelName + ".xlsx");
            // 以字节流的形式输出响应正文
            outputStream = response.getOutputStream();
            try (ExcelWriter excelWriter = EasyExcel.write(outputStream, Supplier.class).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
                excelWriter.write(list, writeSheet);
            }
        } catch (
                IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }
}









